home *** CD-ROM | disk | FTP | other *** search
- /*
- File: TextEditorPrefs.cpp
-
- Contains: TextEditor Preference dialog & file functions
-
- Written by: Steve Smith
-
- Copyright: © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
- */
-
- #ifndef _COMPILERDEFS_
- #include "CompDefs.h"
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _EXCEPT_
- // Exceptions define several important macros (e.g. CHECKENV)
- // which are used in the SOM method dispatch glue. If Except.h
- // is not included early enough, exceptions may not be thrown
- // correctly when returning from a SOM method with "ev" parameter set.
- #include <Except.h>
- #endif
-
- // --- TextEditor Includes ---
-
- #ifndef _TEXTEDITORGLOBALS_
- #include "TextEditorGlobals.h"
- #endif
-
- #ifndef _TEXTEDITORPREFS_
- #include "TextEditorPrefs.h"
- #endif
-
- #ifndef _TEXTEDITORUTILS_
- #include "TextEditorUtils.h"
- #endif
-
- #ifndef _TEXTEDITORDEF_
- #include "TextEditorDef.h"
- #endif
-
- #ifndef _STDTEXTPROPERTIES_
- #include "StdTextProps.h"
- #endif
-
- // --- OpenDoc Includes ---
-
- #ifndef _ODTYPES_
- #include <ODTypes.h>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdProps_defined
- #include <StdProps.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdTypes_defined
- #include <StdTypes.xh>
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- #ifndef SOM_ODContainer_xh
- #include <ODCtr.xh>
- #endif
-
- #ifndef SOM_ODStorageSystem_xh
- #include <ODStor.xh>
- #endif
-
- #ifndef SOM_ODDraft_xh
- #include <Draft.xh>
- #endif
-
- #ifndef SOM_ODDocument_xh
- #include "Document.xh"
- #endif
-
- #ifndef SOM_ODArbitrator_xh
- #include <Arbitrat.xh>
- #endif
-
- #ifndef SOM_ODDispatcher_xh
- #include <Disptch.xh>
- #endif
-
- #ifndef SOM_ODWindowState_xh
- #include <WinStat.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_Foci_defined
- #include <Foci.xh>
- #endif
-
- // --- OpenDoc Utilities
-
- #ifndef _BARRAY_
- #include <BArray.h>
- #endif
-
- #ifndef _ODMEMORY_
- #include <ODMemory.h>
- #endif
-
- #ifndef _USERSRCM_
- #include <UseRsrcM.h>
- #endif
-
- #ifndef _DLOGUTIL_
- #include "DlogUtil.h"
- #endif
-
- #ifndef _ODUTILS_
- #include "ODUtils.h"
- #endif
-
- #ifndef _STDTYPIO_
- #include "StdTypIO.h"
- #endif
-
- #ifndef _TEMPOBJ_
- #include "TempObj.h"
- #endif
-
- // --- Macintosh Includes ---
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
-
- #ifndef __WINDOWS__
- #include <Windows.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef mathRoutinesIncludes
- #include <math routines.h>
- #endif
-
- #ifndef __FP__
- #include <fp.h>
- #endif
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif
-
- #ifndef __OSUTILS__
- #include <OSUtils.h>
- #endif
-
- #ifndef __SCRIPT__
- #include <Script.h>
- #endif
-
- #ifndef __FOLDERS__
- #include <Folders.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
-
- #pragma segment TextEditorPreferences
-
-
- //------------------------------------------------------------------------------
- // ModalPreferencesDialog
- //------------------------------------------------------------------------------
-
- void ModalPreferencesDialog( Environment* ev, ODFrame* frame )
- {
- const ODUShort kFontPopup = 7;
- const ODUShort kSizeField = 8;
-
- TempODPart part = frame->AcquirePart(ev);
- ODSession* session = ODGetSession(ev, part);
-
- // Our About box is modal so we must request the Modal focus to prevent
- // multiple modal dialogs being displayed simultaneously.
-
- if ( session->GetArbitrator(ev)->RequestFocus(ev, gGlobals->fModalFocus, frame) )
- {
- DialogPtr dialog = kODNULL;
- ODVolatile(dialog);
-
- CUsingLibraryResources res;
-
- session->GetWindowState(ev)->DeactivateFrontWindows(ev);
- // Dim the frontmost document window.
-
- TRY
- short iType;
- Handle iHandle;
- Rect iRect;
- ODSShort itemHit;
- Str255 strValue;
- Str255 fontStr;
- ODUShort fontSize;
-
- dialog = GetNewDialog(kPreferencesDialogID, kODNULL, (WindowPtr)-1L);
- THROW_IF_NULL(dialog);
-
- // Set dialog font and other std things.
- SetDialogDefaults(dialog, kDialogFontInfoID, kPrefFontIndex);
-
- // Read in the current "settings" from the preferences file
- ReadPreferences(ev, session, fontStr, &fontSize);
-
- // Get a string representation of the font size and put it in the text field.
- NumToString(fontSize, strValue);
-
- GetDialogItem(dialog, kSizeField, &iType, &iHandle, &iRect);
- SetDialogItemText(iHandle, strValue);
- SelectDialogItemText(dialog, kSizeField, 0, 32767);
-
- // Get the popup menu control.
- ControlHandle popupMenu;
- GetDialogItem(dialog, kFontPopup, &iType, (Handle*) &popupMenu, &iRect);
-
- // Find the initial item for the popup menu and set it.
- {
- PopupPrivateData** privData = (PopupPrivateData**) (**popupMenu).contrlData;
- MenuHandle privMenu = (**privData).mHandle;
-
- short initialItem = 0;
- for (int i = 1; i <= CountMItems(privMenu); i++)
- {
- Str255 menuItem;
- GetMenuItemText(privMenu, i, menuItem);
-
- if ( IUEqualString(fontStr, menuItem) == 0 )
- {
- initialItem = i;
- break;
- }
- }
-
- SetControlValue(popupMenu, initialItem);
- }
-
- // Show the window and handle events in it.
- ShowWindow(dialog);
- ModalFilterUPP otherDialogProc = NewModalFilterProc(OtherDialogFilter);
-
- do
- {
- ModalDialog(otherDialogProc, &itemHit);
- }
- while (itemHit != ok && itemHit != cancel);
-
- DisposeRoutineDescriptor(otherDialogProc);
-
- // If OK was hit, handle the change.
- if ( itemHit == ok )
- {
- // Get the font name.
- short controlValue = GetControlValue(popupMenu);
- PopupPrivateData** privData = (PopupPrivateData**) (**popupMenu).contrlData;
- MenuHandle privMenu = (**privData).mHandle;
-
- GetMenuItemText(privMenu, controlValue, fontStr);
-
- // Get the font size (and make sure it's sane).
- GetDialogItem(dialog, kSizeField, &iType, &iHandle, &iRect);
- GetDialogItemText(iHandle, strValue);
-
- long value = 0;
- if ( strValue[0] > 0 )
- StringToNum(strValue, &value);
-
- fontSize = (ODUShort) value;
-
- if ( value > 127 )
- fontSize = 127;
-
- // Write these to the preferences file.
- WritePreferences(ev, session, fontStr, &fontSize);
- }
-
- CATCH_ALL
- // Cleanup the dialog.
- if (dialog != kODNULL)
- DisposeDialog(dialog);
-
- // Inform the Arbitrator that we no longer require the Modal focus.
- session->GetArbitrator(ev)->RelinquishFocus(ev, gGlobals->fModalFocus, frame);
-
- // Hilite the frontmost document window.
- session->GetWindowState(ev)->ActivateFrontWindows(ev);
-
- RERAISE;
- ENDTRY
-
- // Cleanup the dialog.
- DisposeDialog(dialog);
-
- session->GetArbitrator(ev)->RelinquishFocus(ev, gGlobals->fModalFocus, frame);
- // Inform the Arbitrator that we no longer require the Modal focus.
- session->GetWindowState(ev)->ActivateFrontWindows(ev);
- // Hilite the frontmost document window.
- }
- }
-
- //------------------------------------------------------------------------------
- // GetPreferences
- //------------------------------------------------------------------------------
-
- void GetPreferences( Environment* ev, FSSpec* prefFile,
- ODBoolean* newFile )
- {
- *newFile = kODFalse;
-
- ODGetIndString(prefFile->name, kRuntimeStringsID, kPrefFileNameStrIndex);
- THROW_IF_ERROR(FindFolder(kOnSystemDisk, kPreferencesFolderType,
- kDontCreateFolder,
- &prefFile->vRefNum, &prefFile->parID));
-
- FInfo fInfo;
- OSErr error = FSpGetFInfo(prefFile, &fInfo);
-
- if ( error == fnfErr)
- {
- THROW_IF_ERROR(FSpCreate(prefFile, kTextEditorEditorOSType,
- kPreferencesFolderType,
- gGlobals->fEditorsScript));
-
- *newFile = kODTrue;
- }
- else
- {
- THROW_IF_ERROR(error);
- }
- }
-
- //------------------------------------------------------------------------------
- // GetPrefStorageUnit
- //------------------------------------------------------------------------------
-
- ODBoolean GetPrefStorageUnit( Environment* ev, ODSession* session,
- ODStorageUnit** prefSU )
- {
- ODBoolean result = kODTrue;
-
- FSSpec prefFile;
- ODVolatile(prefFile);
-
- prefFile.vRefNum = 0;
- prefFile.parID = 0;
-
- TRY
- ODBoolean newFile;
- GetPreferences(ev, &prefFile, &newFile);
-
- // Open or create the preferences file.
- ODStorageSystem* storage = session->GetStorageSystem(ev);
-
- TempODByteArray id = CreateByteArray(&prefFile, sizeof(FSSpec));
-
- TempODContainer container = kODNULL;
-
- if ( newFile )
- container = storage->CreateContainer(ev, kODDefaultFileContainer, id);
- else
- container = storage->AcquireContainer(ev, kODDefaultFileContainer, id);
-
- // Get the document, draft, and draft props.
- TempODDocument document = container->AcquireDocument(ev, kODDefaultDocument);
- TempODDraft draft = document->AcquireBaseDraft(ev, kODDPExclusiveWrite);
- TempODStorageUnit draftPropsSU = draft->AcquireDraftProperties(ev);
- TempODStorageUnit su = kODNULL;
-
- // Get the preferences storage unit. If we're making a new file, set it up.
- if ( ODSUExistsThenFocus(ev, draftPropsSU, kTextEditorPreferences, kODStrongStorageUnitRef) )
- {
- ODStorageUnitRef suRef;
- StorageUnitGetValue(draftPropsSU, ev, sizeof(ODStorageUnitRef), &suRef);
-
- su = draft->AcquireStorageUnit(ev, draftPropsSU->GetIDFromStorageUnitRef(ev, suRef));
- }
- else
- {
- // Make sure we have an entry in the draft properties of the prefs file (create
- // one if we don't).
- if ( !draftPropsSU->Exists(ev, kTextEditorPreferences, kODNULL, 0) )
- draftPropsSU->AddProperty(ev, kTextEditorPreferences);
- else
- draftPropsSU->Focus(ev, kTextEditorPreferences, kODPosUndefined, kODNULL, 0, kODPosUndefined);
- if ( !ODSUExistsThenFocus(ev, draftPropsSU, kTextEditorPreferences, kODStrongStorageUnitRef) )
- draftPropsSU->AddValue(ev, kODStrongStorageUnitRef);
-
- // Create a storage unit for our preferences.
- su = draft->CreateStorageUnit(ev);
-
- // Write a reference to it in the draft properties.
- ODStorageUnitRef suRef;
- draftPropsSU->GetStrongStorageUnitRef(ev, su->GetID(ev), suRef);
- StorageUnitSetValue(draftPropsSU, ev, sizeof(ODStorageUnitRef), suRef);
-
- // Write out the changes to the draft.
- draft->Externalize(ev);
- }
-
- *prefSU = su.DontRelease();
-
- // We need to keep a reference to the draft, document, and container
- // for later release if everything else succeeded.
- draft.DontRelease();
- document.DontRelease();
- container.DontRelease();
-
- CATCH_ALL
- // Something bad happened when trying to open the preferences file.
- // we will try to delete the file so that it can be recreated in the
- // future and won't be bad forever.
- // NOTE: We probably want to alert the user to this fact. This is
- // left as an exercise for the reader, however. We don't have that
- // much stuff in our preferences and it's pretty easy to fix, so we
- // won't tell the user.
-
- if ( prefFile.vRefNum != 0 && prefFile.parID != 0 )
- {
- FInfo fInfo;
- OSErr error = FSpGetFInfo(&prefFile, &fInfo);
- if ( error == noErr)
- (void) FSpDelete(&prefFile);
- }
-
- result = kODFalse;
- ENDTRY
-
- return result;
- }
-
- //------------------------------------------------------------------------------
- // ClosePreferences
- //------------------------------------------------------------------------------
-
- void ClosePreferences( Environment* ev, ODStorageUnit* prefSU )
- {
- // We put these in Temp's in order to release them (since they
- // were acquired in GetPrefStorageUnit). We need to use Temps
- // so that they'll be released even if Externalize fails.
- // The order they are released is important, so we create them
- // (on the stack) in the reverse order. This way, they will be
- // released in the order: draft, document, container.
-
- TempODContainer container = kODNULL;
- TempODDocument document = kODNULL;
- TempODDraft draft = kODNULL;
-
- draft = prefSU->GetDraft(ev);
- document = draft->GetDocument(ev);
- container = document->GetContainer(ev);
-
- // Release our storage unit.
- ODSafeReleaseObject(prefSU);
-
- // Update the draft and storage units.
- draft->Externalize(ev);
- }
-
- //------------------------------------------------------------------------------
- // ReadPreferences
- //------------------------------------------------------------------------------
-
- void ReadPreferences( Environment* ev, ODSession* session,
- StringPtr fontName, ODUShort* fontSize )
- {
- long result = GetScriptVariable(smSystemScript, smScriptAppFondSize);
- ODSShort defaultFont = HiWord(result);
- ODSShort defaultSize = LoWord(result);
-
- ODStorageUnit* prefSU;
- if ( GetPrefStorageUnit(ev, session, &prefSU) )
- {
- TRY
- // Get the size from the default font size property.
- if ( prefSU->Exists(ev, kStdProp_DefaultFontSize, kODUShort, 0) )
- *fontSize = ODGetUShortProp(ev, prefSU, kStdProp_DefaultFontSize, kODUShort);
- else
- *fontSize = defaultSize;
-
- TempODIText intlFontName = kODNULL;
- if ( prefSU->Exists(ev, kStdProp_DefaultFont, kODMacIText, 0) )
- intlFontName = ODGetITextProp(ev, prefSU, kStdProp_DefaultFont,
- kODMacIText, kODNULL);
-
- if ( intlFontName == kODNULL || GetITextStringLength(intlFontName) == 0 )
- GetFontName(defaultFont, fontName);
- else
- (void) GetITextPString(intlFontName, fontName);
-
- CATCH_ALL
- ClosePreferences(ev, prefSU);
- RERAISE;
- ENDTRY
-
- ClosePreferences(ev, prefSU);
-
- // Validate preference values.
- ODBoolean updatePrefs = kODFalse;
-
- if ( *fontSize > 127 || *fontSize <= 0 )
- {
- *fontSize = defaultSize;
- updatePrefs = kODTrue;
- }
-
- ODSShort fontNum;
- GetFNum(fontName, &fontNum);
-
- if ( fontNum == 0 )
- {
- // 0 means either the font they want is the system font or that
- // the font was not available. Determine which case this is by
- // checking the font name of the system font (e.g. "Chicago")
- // against the name they were looking for.
-
- Str255 systemFontName;
- GetFontName(0, systemFontName);
-
- if ( IdenticalString(systemFontName, fontName, kODNULL) != 0 )
- {
- // They were looking for a font other than the system font,
- // but it wasn't available, so give them this script's default
- // font.
- GetFontName(defaultFont, fontName);
- updatePrefs = kODTrue;
- }
- }
-
- // Write the udpated values, if necessary.
- if ( updatePrefs )
- WritePreferences(ev, session, fontName, fontSize);
- }
- else
- {
- GetFontName(defaultFont, fontName);
- *fontSize = defaultSize;
- }
- }
-
- //------------------------------------------------------------------------------
- // WritePreferences
- //------------------------------------------------------------------------------
-
- void WritePreferences( Environment* ev, ODSession* session,
- StringPtr fontName, ODUShort* fontSize )
- {
- ODStorageUnit* prefSU;
- if ( GetPrefStorageUnit(ev, session, &prefSU) )
- {
- TRY
- TempODIText intlFontName = CreateITextPString(smRoman, langEnglish, fontName);
-
- ODSetITextProp(ev, prefSU, kStdProp_DefaultFont, kODMacIText, intlFontName);
- ODSetUShortProp(ev, prefSU, kStdProp_DefaultFontSize, kODUShort, *fontSize);
- CATCH_ALL
- ClosePreferences(ev, prefSU);
- RERAISE;
- ENDTRY
-
- ClosePreferences(ev, prefSU);
- }
- }
-
-